﻿package com.thorqq.magictimer.action;

import java.util.Iterator;
import java.util.List;

import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Build;
import android.widget.Toast;

import com.thorqq.magictimer.core.TAction;
import com.thorqq.magictimer.core.TimerAbs;
import com.thorqq.magictimer.util.Util;

/**
 * 清理内存
 * 
 */
public class ActionStopProcess extends TAction
{
    private static final long serialVersionUID = 1448793937067510481L;

    public ActionStopProcess(int id, int execOrder, String actionType, String param)
    {
        super(id, execOrder, actionType, param);
    }

    @Override
    public boolean run(Context context, TimerAbs timer)
    {
        stopProcess(context);

        return true;
    }

    public static void stopProcess(Context context)
    {
        ActivityManager localActivityManager = (ActivityManager) context
                .getSystemService(Context.ACTIVITY_SERVICE);
        long l1 = getUsebleMemory(context, localActivityManager);
        PackageManager localPackageManager = context.getApplicationContext().getPackageManager();
        
        //Returns a list of application processes that are running on the device.
        Iterator<?> localIterator = localActivityManager.getRunningAppProcesses().iterator();
        
        //Return a List of all packages that are installed on the device
        List<PackageInfo> localList = localPackageManager.getInstalledPackages(0);
        
        int cnt = 0;
        while(localIterator.hasNext())
        {
            ActivityManager.RunningAppProcessInfo localRunningAppProcessInfo = 
                (ActivityManager.RunningAppProcessInfo) localIterator.next();

            String runningProcessName = localRunningAppProcessInfo.processName;
            String thisPackageName = context.getPackageName();
            if((runningProcessName.equals(thisPackageName))
                        || (runningProcessName.equals("com.android.phone"))
                        || (runningProcessName.equals("system"))
                        || (runningProcessName.equals("com.htc.launcher"))
                        || (runningProcessName.equals("com.htc.cs")))
            {
                continue;
            }
            
            for(int i = 0; i < localList.size(); i++)
            {
                PackageInfo localPackageInfo = (PackageInfo) localList.get(i);
                if (!localPackageInfo.packageName.equals(runningProcessName)
                        || localPackageInfo.applicationInfo.sourceDir.indexOf("/system/") >= 0)
                {
                    continue;
                }                

                Util.log("Kill process: " + runningProcessName);
                if (Build.VERSION.SDK_INT > 7)
                {
                    localActivityManager.killBackgroundProcesses(runningProcessName);
                }
                else
                {
                    localActivityManager.restartPackage(runningProcessName);
                }
                cnt++;

            }
            
        }
        
        long l2 = getUsebleMemory(context, localActivityManager);
        StringBuilder localStringBuilder = new StringBuilder("清理内存成功,共结束掉 ")
            .append(cnt).append(" 个后台程序,释放内存: ").append(l2 - l1).append("MB");
        Toast.makeText(context, localStringBuilder.toString(), 0).show();
        Util.log(localStringBuilder.toString());
        return;

    }

    public static long getUsebleMemory(Context paramContext, ActivityManager paramActivityManager)
    {
        ActivityManager.MemoryInfo localMemoryInfo = new ActivityManager.MemoryInfo();
        paramActivityManager.getMemoryInfo(localMemoryInfo);
        return localMemoryInfo.availMem / 1048576L;
    }

    @Override
    public String getName()
    {
        return "清理内存";
    }

    @Override
    public String getDescription()
    {
        return null;
    }

    @Override
    public boolean parseStringParam()
    {
        return true;
    }

    @Override
    public String paramToString()
    {
        return null;
    }

}
